<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<th:block th:fragment = "RefundWindow" >
		<link type="text/css" rel="stylesheet" th:href="@{{css}/mobile/member/mobilepersonal/mobileDrawBackCategories.css(css=${css})}"/>
		<!-- 退款弹框 -->
		<div class="weui-screening-categories">
     		<div class="weui-sc-content">
     			<div class="toolbar">
		          <div class="toolbar-inner">
		            <a href="javascript:RefundWindow.closeRefundWindow();" class="picker-button close-popup">关闭</a>
		            <h1 class="title">退款列表</h1>
		          </div>
		        </div>
		        <div class="personal-ordermd-ze">
	    			<div class="weui-cell personal-ordermd-yy">
			            <div class="weui-cell__bd weui-width">
			              <p>退款金额：</p>
			            </div>
			            <div class="weui-cell__ft">￥<em id="refundtotal">0.00</em></div>
				  	</div>
				  	<div class="weui-cell personal-ordermd-yy">
			            <div class="weui-cell__bd weui-width">
			              <p>退款原因：</p>
			            </div>
			            <div class="weui-cell__ft-x"><input id="applydesc" type="text" placeholder="请填写退款原因"></input></div>
				  	</div>
	    		</div>
		        <div class="weui-refund-lists" id="refundgood_list">
		        	<th:block th:fragment="refundgood_list" th:if="${refundgood!=null}">
			        	<div class="weui-tab-buylist-gtime">
			      			<div class="weui-tab-buylist-gtime1"><h1><img th:src="@{{image}/mobile/member/pay-pic.png(image=${image})}"/><em style="font-style:normal" th:text="${refundgood.order.merchantname}"></em></h1></div>
		      			</div>
		      			<div class="weui-cells weui-cells_checkbox">
		      				<th:block th:each="good, i : ${refundgood.orderGoodList}">
			      				<div class="weui-cell weui-refund">
							      <label class="weui-cell weui-check__label-refund" th:for="${!refundgood.orderstate || (refundgood.orderstate && good.supportrefund)} ? 'checkbox_' + ${i.count} : 'sid11'">
							      <!-- <label class="weui-cell weui-check__label-refund" th:for="${!refundgood.orderstate} ? (${good.supportrefund} ? 'checkbox_' + ${i.count} : 'sid11') : 'checkbox_' + ${i.count}"> -->
							        <div class="weui-cell__hd" th:onclick="|RefundWindow.clickSingleSelect('${i.count}');|">
							          <input type="checkbox" class="weui-check" name="checkbox1" th:id="'checkbox_' + ${i.count}">
							          <i class="weui-icon-checked"></i>
							        </div>
							      </label>
							      <div class="weui-tab-buylist-det-refund">
					      			<img th:src="${refundgood.img + good.goodpicture}"/>
					      			<div class="weui-tab-buylist-gdetail-refund">
					      				<span class="weui-tab-buylist-gtit1-refund"><em style="font-style:normal" th:text="${good.goodname}"></em></span>
					      				<span class="weui-tab-buylist-monly-refund">已发货</span>
					      				<input type="hidden" th:id="'good_' + ${i.count}" th:value="${good.entityid}"/>
					      				<input type="hidden" th:id="'supportrefund_' + ${i.count}" th:value="${good.supportrefund}"/>
					      				<input type="hidden" th:id="'paymenttotal_' + ${i.count}" th:value="${good.paymenttotal}"/>
					      			</div>
					      			<div class="weui-tab-monly-refund">	
					      				<span class="weui-tab-buylist-gmoney-refund"><i class="weui-tab-buylist-gm1-refund">￥<em style="font-style:normal" th:text="${good.goodprice}"></em></i><i class="weui-tab-buylist-gm2-refund">X<em style="font-style:normal" th:text="${good.goodnumber}"></em></i></span>
					      			</div>
				      		  	  </div> 
							    </div>
		      				</th:block>
		      			</div>
		      			<script th:inline="javascript">
							var $orderstate = [[${refundgood.orderstate}]]; // 订单状态。 true 标识也完成，f 未完成。
						</script>
		        	</th:block>
		        </div>
		        <div class="weui-payment-bar">
	    			<div class="weui-all-checkbox">
				        <i id="selete-all" class="weui-icon-circle" onclick="RefundWindow.clickAllSelect();"></i>&nbsp;全选
	    			</div>
	    			<div class="weui-settlement" onclick="RefundWindow.executeSave();">提交</div>
	    		</div>
     		</div>
		</div>
		<!--退款弹框-->
		<script th:inline="javascript">
			var RefundWindow = {
				orderid : null,
				popupRefundWindow : function (orderid) { // 弹出退款列表窗口。
					this.orderid = orderid;
					$.showLoading("数据加载中");
					//阻止详情弹框弹出
					var e = window.event || arguments.callee.caller.arguments[0];
					if (e.stopPropagation) {
						e.stopPropagation();
					} else e.cancelBubble = true;
					$(".weui-screening-categories").addClass("weui-screening-categories-show");
					
					$("#refundgood_list").load(__ctx + "/mobiledrawback/getRefundGoodByOrderId.xhtml",
						{orderid: orderid}, function(response,status,xhr){$.hideLoading();}
					);
				},
				closeRefundWindow : function (orderid) { // 关闭退款列表窗口。
					$("#refundtotal").text("0.00");
					$("#applydesc").val("");
					var $selete_all = $("#selete-all").attr("class");
					var flag = $selete_all.indexOf("weui-icon-circle") != -1 ? true : false;
					if (!flag) $("#selete-all").removeClass("weui-icon-success").addClass("weui-icon-circle");
					this.orderid = null;
					$('.weui-screening-categories').removeClass('weui-screening-categories-show');
				},
				clickSingleSelect : function ($id) { // 单选。
					var supportrefund = $("#supportrefund_" + $id).val();
					if ($orderstate && supportrefund == 'false') return;
					var refundtotal = $("#refundtotal").text() == "0.00" ? 0 : parseFloat($("#refundtotal").text());
					var paymenttotal = parseFloat($("#paymenttotal_" + $id).val());
					var $selete_all = $("#selete-all").attr("class");
					var flag = $selete_all.indexOf("weui-icon-circle") != -1 ? true : false;
					if ($("#checkbox_" + $id).prop("checked")) { // 点击选中累加金额。
						refundtotal += paymenttotal;
						var _istate = true;
						$("input[type='checkbox']").each(function(i, v){
							var $index = $(this).attr("id").split("_")[1]; // 获取生成每条商品信息的索引值。
							if (!$orderstate) {
								if (!$(this).prop("checked")) {
									_istate = false;
									return false;
								}
							} else {
								var supportrefund = $("#supportrefund_" + $index).val();
								if (supportrefund == 'false') {
									_istate = false;
									return false;
								}
							}
							if (flag && _istate) $("#selete-all").removeClass("weui-icon-circle").addClass("weui-icon-success"); // 触发全选。
					    });
					} else {
						refundtotal = refundtotal - paymenttotal;
						if (!flag) $("#selete-all").removeClass("weui-icon-success").addClass("weui-icon-circle");
					}
					$("#refundtotal").text(refundtotal.toFixed(2)); // 总价。
				},
				clickAllSelect : function () { // 全选。
					var $selete_all = $("#selete-all").attr("class");
					var flag = $selete_all.indexOf("weui-icon-circle") != -1 ? true : false;
					var _istate = true;
					var refundtotal = $("#refundtotal").text() == "0.00" ? 0 : parseFloat($("#refundtotal").text());
					$("input[type='checkbox']").each(function(i, v){
						var $index = $(this).attr("id").split("_")[1]; // 获取生成每条商品信息的索引值。
						var supportrefund = $("#supportrefund_" + $index).val();
						var paymenttotal = parseFloat($("#paymenttotal_" + $index).val());
						if ($orderstate && supportrefund == 'false') {
							_istate = false;
							return true;
						}
						if (flag && !$(this).prop("checked")) { // 检查是否勾选, 无勾选打勾。
							$(this).prop("checked", "checked"); 
							refundtotal += paymenttotal;
						} else if (!flag && $(this).prop("checked")) { // 去掉全选, 清楚商品已打勾项。
							$(this).removeAttr("checked");
							refundtotal = refundtotal - paymenttotal;
						} else return true;
				    });
					if (flag && _istate) $("#selete-all").removeClass("weui-icon-circle").addClass("weui-icon-success"); // 触发全选。
					else  $("#selete-all").removeClass("weui-icon-success").addClass("weui-icon-circle");
					$("#refundtotal").text(refundtotal.toFixed(2)); // 总价。
				},
				executeSave : function () { // 保存。
					var applydesc = $("#applydesc").val();
					if (applydesc === null || applydesc === "") {
						mobileGlobal.warn("请填写退款原因！");
						return ;
					}
					var orderGoodId = new Array();
					$("input[type='checkbox']").each(function(i, v){
						var $index = $(this).attr("id").split("_")[1];
						if ($(this).prop("checked")) {
							orderGoodId.push($("#good_" + $index).val());
						}
				    });
					if (orderGoodId == null || orderGoodId.length == 0) { // 无勾选商品。
						mobileGlobal.warn("请勾选需退款的商品！");
						return ;
					}
					var param = {orderid: RefundWindow.orderid, ordergoodid: orderGoodId.join(","), applydesc: applydesc};
					mobileGlobal.sendRequest(__ctx + "/mobiledrawback/executeRefundByOrderId.xhtml",  param, "数据加载中", function(result){
						console.log(result);
						if(result.status){
							mobileGlobal.warn("申请成功！");
							$("#refundtotal").text("0.00");
							$("#applydesc").val("");
							refreshData();
							RefundWindow.closeRefundWindow();
						} else mobileGlobal.warn(result.result.msg);
					});
				}
			}
		</script>
	</th:block>
</html>